/********************************Copyright (c)**********************************\
**
**                   (c) Copyright 2019, Main, China, Mrtutu.
**                           All Rights Reserved
**
**                                By Mrtutu
**                           Blog: www.mrtutu.cn
**
**----------------------------------文件信息------------------------------------
** @flie    : timer.c
** @auther  : Mrtutu
** @date    : 2019-11-18
** @describe:
**
**----------------------------------版本信息------------------------------------
** 版本代号: V0.1
** 版本说明: 初始版本
**
**------------------------------------------------------------------------------
\********************************End of Head************************************/

#include "timer.h"


u16 TIM_CNT_a   = 0;
u16 TIM_CNT_m   = 0;
u16 TIM_CNT_M = 10;
u16 TIM_CNT_A = 10;
u8  mflag = 0;
u8  aflag = 0;


void TIM3_Int_Init(u16 arr, u16 psc)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //①时钟 TIM3 使能

    TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
    TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置时钟频率除数的预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始化 TIM3
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //允许更新中断
    //中断优先级 NVIC 设置
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3 中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级 0 级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级 3 级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能
    NVIC_Init(&NVIC_InitStructure); //初始化 NVIC 寄存器
    TIM_Cmd(TIM3, ENABLE); //使能 TIM3
}



//定时器 3 中断服务程序
void TIM3_IRQHandler(void) //TIM3 中断
{
    if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //检查 TIM3 更新中断发生与否
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除 TIM3 更新中断标志
        
        // 电机
        if(mflag)
        {
            aflag     = 0; // 互锁
            TIM_CNT_a = 0;
            //printf("Motor Runing... \r\n");
            TIM_SetCompare2(TIM4, 100);   // 电机A
            // 定时到
            if(TIM_CNT_m > TIM_CNT_M)
            {
                /*操作*/ 
                //printf("Motor Stop... \r\n");
                TIM_SetCompare2(TIM4, 0); // 电机A
                TIM_CNT_m = 0;
                aflag     = 1;
                mflag     = 0;
            }
            
             TIM_CNT_m++;
        }
        
        // 电磁阀
        if(aflag)
        {
            mflag     = 0; // 互锁
            TIM_CNT_m = 0;
            //printf("Switch OFF... \r\n");
            Relay_ctl(0);
            // 1s定时到
            if(TIM_CNT_a > TIM_CNT_A)
            {
                /*操作*/
                //printf("Switch ON... \r\n");
                Relay_ctl(1);     
                TIM_CNT_a = 0;
                aflag     = 0;
                mflag     = 1;
            }
            
            TIM_CNT_a++;
        } 
    }
}



/********************************End of File************************************/